package com.turnengine.client.local.player.command;

import com.javabi.command.IExecutableCommand;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.serializer.AbstractCommandDataSerializer;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import java.io.IOException;

/**
 * The Rename Player Data Serializer.
 */
public class RenamePlayerDataSerializer extends AbstractCommandDataSerializer<String> implements IRenamePlayerDataSerializer {

	/**
	 * Creates a new Rename Player Data Serializer.
	 */
	public RenamePlayerDataSerializer() {
		super(RenamePlayer.COMMAND_ID);
	}

	@Override
	public String readReturnValue(IDataReader reader) throws IOException {
		String value = reader.readString(true);
		return value;
	}

	@Override
	public void writeReturnValue(IDataWriter writer, String value) throws IOException {
		writer.writeString(value, true);
	}

	@Override
	public IExecutableCommand<String> readRequest(IDataReader reader) throws IOException {

		// Read fields
		long loginId = reader.readLong();
		int instanceId = reader.readInt();
		String from = reader.readString(true);
		String to = reader.readString(true);

		// Create object
		RenamePlayer object = new RenamePlayer();
		object.setLoginId(loginId);
		object.setInstanceId(instanceId);
		object.setFrom(from);
		object.setTo(to);
		return object;
	}

	@Override
	public void writeRequest(IDataWriter writer, IExecutableCommand<String> argument) throws IOException {
		RenamePlayer object = (RenamePlayer) argument;

		// Get fields
		long loginId = object.getLoginId();
		int instanceId = object.getInstanceId();
		String from = object.getFrom();
		String to = object.getTo();

		// Write fields
		writer.writeLong(loginId);
		writer.writeInt(instanceId);
		writer.writeString(from, true);
		writer.writeString(to, true);
	}

	@Override
	public IExecutableCommandResponse<String> newResponse() {
		return new RenamePlayerResponse();
	}
}
